import { isUndefined, isType } from '../utils'
class ComHook{
  init(){
    this.once('onFirstRendered', (model) => {
      return isType(this.onFirstRendered, 'Function') ? this.onFirstRendered() : null
    })
    this.on('onRendered', ()=>{
      return isType(this.onRendered, 'Function') ? this.onRendered() : null
    })
    this.on('onRendered', this.initElEvents.bind(this), 'last' )
    this.on('onCreated', ()=>{
      return isType(this.onCreated, 'Function') ? this.onCreated() : null
    })
    this.on('onBeforeTemplate', ( model ) => {
      return isType(this.onBeforeTemplate, 'Function') ? this.onBeforeTemplate(model) : null
    })
    this.on('onBeforeRender', ( tpl ) => {
      return isType(this.onBeforeRender, 'Function') ? this.onBeforeRender( tpl ) : null
    })    
    //onAfterRendered
    this.on('onAfterRendered', ( tpl ) => {
      return isType(this.onAfterRendered, 'Function') ? this.onAfterRendered( tpl ) : null
    })
  }

  initElEvents(){
    let defines = this.elEvents
    const that  = this
    if( this.constructor.__elevents__){
      defines = defines || {}
      this.constructor.__elevents__.forEach(evs => {
        defines = Object.assign({}, evs, defines )
      });
    }
    if(defines){
      for( let key in defines ){
        let handle = defines[key]
        let ord = key.replace(/\s{1,}/g, '')
        ord = ord.replace(/\,{1,}/g, ',')
        let mths = ord.split('|')
        ord = mths[0].split('@')
        mths = mths[1]

        mths = mths ? mths.toLowerCase().split(',') : []

        if(isType(handle, 'String')){
          handle = handle.replace(/\s{1,}/g, '')
          handle = isType( that[handle], 'Function' ) ? that[handle].bind(that) : ()=>{
            console.log('Cannt found the method:' + handle)
          }
        }else if( isType(handle, 'Function') ){
          handle.bind(this)
        }else{
          handle = ()=>{
            console.log('Cannt found the method:' + handle)
          }
        }

        if( ord.length === 2){
          const sels = ord[1].split(',')
          const enames = ord[0].split(',')
          const docreg = /^\{(.{1,})\}$/g
          sels.map((sel)=>{
            let oel = null
            docreg.lastIndex = 0
            if(docreg.test(sel)){
              docreg.lastIndex = 0
              const met = docreg.exec(sel)
              if(met && met.length>=2){
                let s = met[1]
                if( s == 'document'){
                  oel = $(document)
                }else{
                  oel = $(s)
                }
              }
            }
            oel = oel ? oel : (sel ? this.root.find(sel) : this.root)
            enames.map((ename)=>{
              oel.unbind(ename)
              if(oel[ename]){
                oel[ename](e => {
                  handle.apply( this , [e])
                  mths.indexOf('propagation')<0 && e.stopPropagation && e.stopPropagation()
                  mths.indexOf('default')<0 && e.preventDefault && e.preventDefault()
                })
              }else if(oel.on){
                oel.on( ename,  e => {
                  handle.apply( this , [e])
                  mths.indexOf('propagation')<0 && e.stopPropagation && e.stopPropagation()
                  mths.indexOf('default')<0 && e.preventDefault && e.preventDefault()
                })
              }
            })
          })
        }
      }
    }
  }
}

export default ComHook